<?php
App::uses('AppController', 'Controller');
/**
 * UserBuyProducts Controller
 *
 * @property UserBuyProduct $UserBuyProduct
 */
class UserBuyProductsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->UserBuyProduct->recursive = 0;
		$this->set('userBuyProducts', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->UserBuyProduct->exists($id)) {
			throw new NotFoundException(__('Invalid user buy product'));
		}
		$options = array('conditions' => array('UserBuyProduct.' . $this->UserBuyProduct->primaryKey => $id));
		$this->set('userBuyProduct', $this->UserBuyProduct->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$postData = $this->request->data;
			
			$postCase =  $postData['UserBuyProduct']['case'];
			$userId = $postData['UserBuyProduct']['user_id'];
			$productId = $postData['UserBuyProduct']['product_id'];
			$quantity = $postData['UserBuyProduct']['quantity'];
			
			$result = $this->UserBuyProduct->buy_case($userId, $productId, $quantity, $postCase);
			
			if ($result) {
				$this->Session->setFlash(__('The user buy product has been saved'));
//				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user buy product could not be saved. Please, try again.'));
			}
		}
		
		$users = $this->UserBuyProduct->User->find('list', array('fields' => array('User.user_name')));
		$products = $this->UserBuyProduct->Product->find('list', array('fields' => array('Product.product_name')));
		$this->set(compact('users', 'products'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->UserBuyProduct->exists($id)) {
			throw new NotFoundException(__('Invalid user buy product'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->UserBuyProduct->save($this->request->data)) {
				$this->Session->setFlash(__('The user buy product has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The user buy product could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('UserBuyProduct.' . $this->UserBuyProduct->primaryKey => $id));
			$this->request->data = $this->UserBuyProduct->find('first', $options);
		}
		$users = $this->UserBuyProduct->User->find('list');
		$products = $this->UserBuyProduct->Product->find('list');
		$this->set(compact('users', 'products'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->UserBuyProduct->id = $id;
		if (!$this->UserBuyProduct->exists()) {
			throw new NotFoundException(__('Invalid user buy product'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->UserBuyProduct->delete()) {
			$this->Session->setFlash(__('User buy product deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('User buy product was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
